########################################
#### figure_6.R: Generates Figure 6 ####
########################################

q96 <- read.csv("../survey data/midterm_fraud_open_ends96.csv", stringsAsFactors=FALSE)
q96<-q96[,c(1,3)]
df <-merge(dta_midterm,q96)

df$w9_2022_1_elections96_code[df$w9_2022_1_elections96_code=="99"] <-8
df$w9_2022_1_elections96_code[df$w9_2022_1_elections96_code=="51"] <-5
df$w9_2022_1_elections96_code[df$w9_2022_1_elections96_code=="52"] <-5
df$w9_2022_1_elections96_code[df$w9_2022_1_elections96_code=="53"] <-5
df$w9_2022_1_elections96_code[df$w9_2022_1_elections96_code=="54"] <-5
results <- pollster::moe_crosstab(df, y=w9_2022_1_elections96_code, x=pid, weight=weight) %>%
  mutate(upper = pct+moe) %>%
  mutate(lower = pct-moe) %>%
  mutate(wn = round(n*(pct/100),0))
results <- results %>% add_row(pid = "Democrat", w9_2022_1_elections96_code = "3", pct =0, upper=0, lower=0)
results <- results %>% add_row(pid = "Democrat", w9_2022_1_elections96_code = "7", pct =0, upper=0, lower=0)

results$w9_2022_1_elections96_code <- recode(results$w9_2022_1_elections96_code, 
                                             '1'= "Money/power",
                                             '2'= "Corruption",
                                             '3'= "No prosecution/investigation", 
                                             '4' = "Conspiracy theory",
                                             '5' = "Bad procedures allow it",
                                             '6' = "Media support",
                                             '7' = "Because of Democrats", 
                                             '8' = "Other")
results$q=1
results$w9_2022_1_elections96_code = as.factor(results$w9_2022_1_elections96_code)

results<- results %>% 
  arrange(pct)

fig_6 = results  %>% mutate(pid = as.factor(pid),
                    level = tidytext::reorder_within(w9_2022_1_elections96_code, pct, pid)) %>%
  ggplot(aes(x=level, y= pct, color=pid)) +
  geom_point(aes(y = pct), position = position_dodge()) +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=0, position=position_dodge(.9)) +
  geom_text(aes(y= pct,label = paste(round(pct,1),"%",sep="")), fontface = "bold", vjust = 1.5,
            position = position_dodge(1), size = 4) +
  xlab("") +
  scale_y_continuous(limits = c(-10,62), name = "Percent",breaks = seq(0,100,50), labels = paste0(seq(0,100,50),"%")) +
  coord_flip() +
  tidytext::scale_x_reordered() +
  facet_wrap(~pid, scales = "free_y") +
  
  scale_color_manual(values=c("Republican" = "#D50A0A","Democrat" ="#013369", "Independent"="#999999")) +
  guides(fill="none", color="none") +
  ggtitle(wrapper("",60)) +
  theme_prl()+
  guides(fill=guide_legend(title="",reverse = TRUE, byrow=TRUE)) +
  theme(panel.grid.major = element_blank(), legend.position = "none") +
  labs(caption = "Weighted values and 95% CIs") 

ggsave(here("Figures","figure_6.pdf"), fig_6,
            dpi = 600, units = "in", width=10, height=5)
       